import $http from '../utils/http.js'
import { Notification } from 'atelement'
const register = 'register/register'
const userList = 'users/getlist'
const resetPass = 'users/resetPass'
const deleteUser = 'users/deleteUser'
const companys = 'users/getDepartments'
const changeFormData = 'users/changeFormData'
const handleSizeChange = 'users/changePageSize'
const handleCurrentChange = 'users/changeCurrent'
const changeBegin = 'users/changeInfoBegin'
const getRoles = 'users/getRoles'
const changeSearchInput = 'users/changeSearchInput'
const settersLogin = 'users/settersLogin'
const changeSort = 'users/changeSort'
const changeLoading = 'users/changeLoading'
let baseSearch = {
  pagination: {
    current: 1, // 当前页码
    pageSize: 10 // 当前页显示的数据条数
  },
  search: {
    value: '' // 需要模糊搜索的值（搜索框）
  },
  sorter: {
    field: 'br_time',
    order: 'descend'
  }
}

export default {
  state: {
    formInfo: {
      key: '',
      personName: '',
      sex: '',
      birthday: '',
      userName: '',
      roleName: '',
      department: '',
      email: '',
      mobilePhone: '',
      remark: ''
    },
    changeFormInfo: {
      key: '',
      personName: '',
      sex: '',
      birthday: '',
      userName: '',
      roleName: '',
      department: '',
      email: '',
      mobilePhone: '',
      remark: ''
    },
    tableData: [],
    currentPage: 1,
    total: -1,
    roleList: [],
    departmentList: [],
    searchInput: '',
    newUsersControl: false,
    pageSize: 10,
    fullscreenLoading: false
  },

  actions: {
    [changeSort] ({ dispatch, commit }, { prop, order }) {
      baseSearch.sorter.field = prop
      baseSearch.sorter.order = order.slice(0, -3)
      dispatch(userList)
    },
    [register] ({ dispatch, commit }, sendData) {
      $http.post('/api/user', sendData).then(({ status, msg }) => {
        if (status === 'success') {
          if (sendData.key === null) {
            Notification.success({
              title: '新用户添加成功'
            })
            dispatch(userList)
          } else {
            Notification.success({
              title: '用户信息修改成功'
            })
            commit('system/settersRoot', {
              key: 'showModelChangeInfo',
              value: false
            })
            dispatch('system/auth')
            dispatch(userList)
          }
        } else if (status === 'fail') {
          if (sendData.key === null) {
            Notification.error({
              title: '用户添加失败',
              message: msg
            })
          } else {
            Notification.error({
              title: '用户信息修改失败',
              message: msg
            })
          }
        }
      })
    },
    [userList] ({ commit }) {
      commit(changeLoading)
      $http.option('/api/users', baseSearch).then(({ status, msg, data }) => {
        if (status === 'success') {
          commit(userList, data)
          commit(changeLoading)
        } else if (status === 'fail') {
          Notification.error({
            title: '数据获取异常',
            message: msg
          })
        }
      })
    },
    [resetPass] (none, data) {
      $http.put('/api/user/' + data).then(({ status, msg }) => {
        if (status === 'success') {
          Notification.success({
            title: '密码重置成功'
          })
        } else if (status === 'fail') {
          Notification.error({
            title: '密码重置失败',
            message: msg
          })
        }
      })
    },
    [deleteUser] ({ dispatch }, data) {
      $http.delete('/api/user/' + data).then(({ status, msg }) => {
        if (status === 'success') {
          Notification.success({
            title: '用户删除成功'
          })
          dispatch(userList)
        } else if (status === 'fail') {
          Notification.error({
            title: '用户删除失败',
            message: msg
          })
        }
      })
    },
    [companys] ({ commit }) {
      // $http.get('/api/company/show').then(({ status, msg, data }) => {
      //   if (status === 'success') {
      //     commit(companys, data)
      //   } else if (status === 'fail') {
      //     Notification.error({
      //       title: '部门信息获取失败',
      //       message: msg
      //     })
      //   }
      // })
    },
    [handleSizeChange] ({ dispatch, commit }, data) {
      baseSearch.pagination.pageSize = data
      commit(handleSizeChange, data)
      dispatch(userList)
    },
    [handleCurrentChange] ({ dispatch, commit }, data) {
      baseSearch.pagination.current = data
      dispatch(userList)
    },
    [getRoles] ({ commit }) {
      $http.get('/api/user/role').then(({ status, msg, data }) => {
        if (status === 'success') {
          commit(getRoles, data)
        } else if (status === 'fail') {
          Notification.error({
            title: '角色信息获取失败',
            message: msg
          })
        }
      })
    }
  },
  mutations: {
    [handleSizeChange] (state, data) {
      state.pageSize = data
    },
    [companys] (state, data) {
      state.departmentList = data.node_data
    },
    [getRoles] (state, data) {
      state.roleList = data
    },
    [userList] (state, data) {
      state.tableData = data.dataSource
      state.currentPage = data.current
      state.total = data.total
    },
    [changeFormData] (state, data) {
      state.formInfo.key = data.key
      state.formInfo.personName = data.bperson_name
      state.formInfo.userName = data.auser_name
      state.formInfo.roleName = data.drole_name
      state.formInfo.department = data.fnode_id
      state.formInfo.email = data.bemail
      state.formInfo.mobilePhone = data.bmobile_phone
      state.formInfo.remark = data.bremark
      state.formInfo.sex = data.bsex
      state.formInfo.birthday = data.bbirthday
    },
    [changeBegin] (state, data) {
      state.changeFormInfo.key = data.key
      state.changeFormInfo.personName = data.bperson_name
      state.changeFormInfo.userName = data.auser_name
      state.changeFormInfo.roleName = data.drole_name
      state.changeFormInfo.department = data.fnode_id
      state.changeFormInfo.email = data.bemail
      state.changeFormInfo.mobilePhone = data.bmobile_phone
      state.changeFormInfo.remark = data.bremark
      state.changeFormInfo.sex = data.bsex
      state.changeFormInfo.birthday = data.bbirthday
    },
    [changeSearchInput] (state, data) {
      state.searchInput = data
      baseSearch.search.value = data
    },
    [settersLogin] (state, data) {
      state.login[data.key] = data.value
    },
    [changeLoading] (state) {
      if (state.fullscreenLoading === true) {
        state.fullscreenLoading = false
      } else {
        state.fullscreenLoading = true
      }
    }
  }
}
